//package com.yunatt.uitls;
//
//import com.sun.net.httpserver.Headers;
//import com.sun.net.httpserver.HttpServer;
//import com.sun.net.httpserver.HttpExchange;
//import com.sun.net.httpserver.HttpHandler;
//
//import java.io.*;
//import java.net.InetSocketAddress;
//
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.StandardCopyOption;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//public class FileUploadServer {
//
//    public static void main(String[] args) throws IOException {
//        int port = 8000;
//        HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
//        server.createContext("/upload", new UploadHandler());
//        server.setExecutor(null);
//        server.start();
//        System.out.println("Server started on port " + port);
//    }
//
//    static class UploadHandler implements HttpHandler {
//        @Override
//        public void handle(HttpExchange exchange) throws IOException {
//
//            if ("POST".equals(exchange.getRequestMethod())) {
//                Headers headers = exchange.getRequestHeaders();
//                String contentType = headers.getFirst("Content-Type");
//
//
//                if (contentType != null && contentType.startsWith("multipart/form-data")) {
//                    InputStream is = exchange.getRequestBody();
//                    Path tempFile = Files.createTempFile("uploaded", ".zip");
//                    Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING);
//
//                    String response = "File uploaded successfully to: " + tempFile.toAbsolutePath();
//
//                    exchange.sendResponseHeaders(200, response.length());
//                    try (OutputStream os = exchange.getResponseBody()) {
//                        os.write(response.getBytes());
//                    }
//                } else {
//                    exchange.sendResponseHeaders(400, -1); // Bad Request
//                }
//            }else if ("GET".equals(exchange.getRequestMethod())) {
//                String query = exchange.getRequestURI().getQuery();
//                System.out.println(query);
//                Map<String, String> params = new HashMap<>();
//
//                for (String param : query.split("&")) {
//                    String[] pair = param.split("=");
//                    if (pair.length > 1) {
//                        params.put(pair[0], pair[1]);
//                    }
//                }
//                System.out.println(params.get("name"));
//                String name = params.get("name");
//                String path = "";
//                String lowerCase = System.getProperty("os.name").toLowerCase();
//                if (lowerCase.contains("windows")) {
//                    System.out.println("当前操作系统是 Windows");
//                    path="E:\\home\\"+name;
//
//                } else {
//                    System.out.println("当前操作系统不是 Windows");
//                    path="/home/file"+name;
//
//                }
//                System.out.println(path);
//                File file = new File(path);
//
//                if (!file.exists()) {
//                    String response = "File not found";
//                    exchange.sendResponseHeaders(404, response.getBytes().length);
//                    OutputStream os = exchange.getResponseBody();
//                    os.write(response.getBytes());
//                    os.close();
//                } else {
//                    exchange.sendResponseHeaders(200, file.length());
//                    OutputStream os = exchange.getResponseBody();
//                    Files.copy(file.toPath(), os);
//                    os.close();
//                }
//
//
//            }else {
//                exchange.sendResponseHeaders(405, -1); // Method Not Allowed
//            }
//            exchange.close();
//        }
//    }
//}